package com.zlcx.tz.live.processor.impl;

import com.zlcx.tz.live.common.config.AsyncConfig;
import com.zlcx.tz.live.domain.TimeLearnRoomDO;
import com.zlcx.tz.live.processor.TimeLearnRoomPostProcessor;
import com.zlcx.tz.live.service.impl.TimeLearnRoomServiceImpl;
import com.zlcx.tz.live.vo.TimeLearnRoomSeatsDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author: anjl
 * @description: （异步）更新计时自习室房间信息->房间总座位数和房间空余座位数
 * @data: 2020-09-27
 **/
@Slf4j
@Order(90)
@Component
public class RefreshRoomInfoPostProcessor implements TimeLearnRoomPostProcessor<TimeLearnRoomSeatsDTO> {

    @Override
    public void processor(TimeLearnRoomSeatsDTO seat) {
        Asserts.notNull(seat, "roomUser is null !");
        Long roomId = seat.getRoomId();
        schedulerExecutor.schedule(() -> {
            TimeLearnRoomDO existRoom = timeLearnRoomService.lockRoom(roomId);
            existRoom.setUsedSeatsNum(timeLearnRoomService.usedSeatsNum(roomId));
            timeLearnRoomService.save(existRoom);
            log.debug("计时自习室-更新自习室人数信息 existRoom = {}", existRoom);
        }, AsyncConfig.getDelayDate(1, TimeUnit.SECONDS));
    }

    @Autowired
    private TimeLearnRoomServiceImpl timeLearnRoomService;
    @Resource(name = "schedulerExecutor")
    private ThreadPoolTaskScheduler schedulerExecutor;

}
